package com.company.cloud.pay.modules.mgr.controller;

import java.util.function.Function;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.CheckUtils;
import com.company.cloud.pay.modules.base.entity.PayThirdCallLog;
import com.company.cloud.pay.modules.base.service.PayThirdCallLogService;
import com.company.cloud.pay.modules.cache.model.PayThirdCallLogModel;
import com.company.cloud.pay.modules.cache.utils.PayConvertUtils;
import com.company.cloud.pay.modules.mgr.controller.req.PayThirdCallLogQuery4PageParam;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 第三方支付调用日志查询
 */
@Authc
@RestController
@RequestMapping("/mgr/pay/payThirdCallLog")
@Api(tags="Pay_Mgr_第三方支付调用日志查询")
public class LogCallPayMgrController {
	
	@Autowired
	private PayThirdCallLogService payThirdCallLogService;

	@PostMapping("/query")
	@ApiOperation(value="查询",response=PayThirdCallLogModel.class)
	public BaseResult query(@RequestBody PayThirdCallLogQuery4PageParam params) {
		IPage<PayThirdCallLog> page = payThirdCallLogService.page(
				new Page<PayThirdCallLog>(params.getPage(), params.getRows()),
				new QueryWrapper<PayThirdCallLog>()
				.eq(StringUtils.hasText(params.getCompanyNo()), "company_no",params.getCompanyNo())
				.eq(StringUtils.hasText(params.getServiceCode()), "service_code",params.getServiceCode())
				.eq(StringUtils.hasText(params.getPayChannelNo()), "pay_channel_no",params.getPayChannelNo())
				.eq(StringUtils.hasText(params.getPayWay()), "pay_way",params.getPayWay())
				.eq(StringUtils.hasText(params.getThirdMethodCode()), "third_method_code",params.getThirdMethodCode())
				.like(StringUtils.hasText(params.getThirdMethodName()), "third_method_name",params.getThirdMethodName())
				.orderByDesc("cdt")
				);
		IPage<PayThirdCallLogModel> data = page.convert(new Function<PayThirdCallLog, PayThirdCallLogModel>() {
			@Override
			public PayThirdCallLogModel apply(PayThirdCallLog entity) {
				return PayConvertUtils.getPayThirdCallLogModel(entity);
			}
		});
		return BaseResult.ok(data);
	}
	
	
	@GetMapping("/get")
	@ApiOperation(value="详细",response=PayThirdCallLogModel.class)
	public BaseResult get(@RequestParam("id")Integer id) {
		if(CheckUtils.isEmpty(id)) {
			throw new BusinessException(BaseReturnEnum.PARAM_NULL);
		}
		PayThirdCallLog entity= payThirdCallLogService.getById(id);
		if(entity==null) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		PayThirdCallLogModel data= PayConvertUtils.getPayThirdCallLogModel(entity);
		return BaseResult.ok(data);
	}
	
	
	
	
}
